//
//  SEGPayLogYearListViewController.m
//  ExcellentHome
//
//  Created by 刘山 on 16/12/8.
//  Copyright © 2016年 陈区. All rights reserved.
//
#import "SEGBillHeaderViewCell.h"
#import "SEGPayLogYearDeatilViewController.h"
#import "SEGPayLogYearListViewController.h"
#import "SEGNewBillManager.h"

@interface SEGPayLogYearListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SEGNewBillManager *billManager;
@property (nonatomic, strong) NSArray<SEGPayLogModel *> *dataDource;

@end

@implementation SEGPayLogYearListViewController {
    NSInteger payLogYear;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleNavigation];
    
    payLogYear = [[SEGUtils timeForTodayWithType:SEGSystemUtils_Time_Today_Type_Year] integerValue];

    [self initPayLogYearList];

    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [weakSelf loadData];
    }];
    [self.tableView beginHeaderRefreshing];
}

// 处理导航栏栈数据
- (void)handleNavigation {
    NSMutableArray<UIViewController *> *temp = [NSMutableArray array];
    for (UIViewController *vc in self.navigationController.viewControllers) {
        Class didPaidVCClass = [[SEGUhomeMediator sharedInstance] seg_getDidPayViewControllerClass:nil];
        if (![vc isKindOfClass:didPaidVCClass]) {
            [temp addObject:vc];
        }
    }
    self.navigationController.viewControllers = [temp copy];
}

#pragma mark - 加载数据
- (void)loadData
{
    if (self.houseId.length == 0) {
        [SVProgressHUD dismiss];
        [self.tableView stopAnimatingWindmill];
        return;
    }
    
    NSDictionary* params = @{
        @"payLogYear" : @(payLogYear),
        @"houseId" : self.houseId,
        @"custId" : self.custId,
        @"custType" : self.custType
    };

    [SVProgressHUD show];
    WeakSelf;
    [self.billManager requestBillPaymentRecoed:params finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
        [SVProgressHUD dismiss];
        [weakSelf.tableView stopAnimatingWindmill];
        if (!isSuccessed) {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
        
        weakSelf.dataDource = [weakSelf.billManager.payRecodeModels copy];
        // 显示空数据提示界面
        if (weakSelf.dataDource.count == 0) {
            [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":weakSelf.tableView, @"text":@"暂无缴费记录", @"image":@"pic_default_nobills"}];
        } else {
            [[SEGUhomeMediator sharedInstance] seg_hideCustomProgressImage:@{@"view":weakSelf.tableView, @"text":@"暂无缴费记录", @"image":@"pic_default_nobills"}];
            [weakSelf.tableView reloadData];
        }
    }];
}

- (void)initPayLogYearList
{
    self.title = @"缴费记录";
    self.view.backgroundColor = kColor_bg;
    [self showCustomNavigationBackButton];
    [self.view addSubview:self.tableView];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.tableView registerClass:[SEGBillHeaderViewCell class] forCellReuseIdentifier:SEGBillHeaderViewCellIndentifier];
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataDource.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

    if (indexPath.row < self.dataDource.count) {

        SEGPayLogModel* data = self.dataDource[indexPath.row];

        SEGBillHeaderViewCell *cell = [[SEGBillHeaderViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SEGBillHeaderViewCellIndentifier WithStype:SEGBillHeaderViewCellType_PayLog_List];

        cell.ContentLabel.text = data.payDate;

        NSString* part = [NSString stringWithFormat:@"(%@)", data.payMethodName];
        NSString* contents = [NSString stringWithFormat:@"%@%@", [NSString stringWithFormat:@"%.2f", [data.payAmont floatValue] * MoneyRateChange], part];
        cell.ContentLabel1.attributedText = [cell stringWithAllContents:contents partContent:part];

        cell.ContentLabel2.text = data.address;
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
            [cell layoutIfNeeded];
        }

        return cell;
    }

    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    return cell;
}

#pragma mark -UITableViewDelegate

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        return UITableViewAutomaticDimension;
    }

    return 44;
}

- (CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return 100.0;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    SEGPayLogYearDeatilViewController* VC = [[SEGPayLogYearDeatilViewController alloc] init];
    VC.houseId = self.houseId;
    VC.dataSource = self.dataDource[indexPath.row];
    VC.custId = self.custId;
    VC.custType = self.custType;

    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - lazy load

- (UITableView*)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kColor_bg;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (SEGNewBillManager *)billManager {
    if (!_billManager) {
        _billManager = [[SEGNewBillManager alloc] init];
    }
    return _billManager;
}

@end
